#include "cJSON.h"
#include <stdio.h>

int main(int argc, char const *argv[])
{

    // 1 把字符串json 转换为cJSON 提取其中的值 
    // 2 随便生成一个json字符串
    char json[] = "{\"name\":\"zhangsan\",\"age\":18,\"sex\":\"man\"}";
    //  3 转成cJson结构体  提取对应的元素cjson  元素cjson提取值
    cJSON *root = cJSON_Parse(json);
    if (root == NULL)
    {
        printf("cjson parse error\n");
    }   
    cJSON *name_cjson = cJSON_GetObjectItem(root, "name");
    cJSON *age_cjson = cJSON_GetObjectItem(root, "age");
    char *name= name_cjson->valuestring;
    printf("name:%s\n",name);
    printf("age:%d\n",age_cjson->valueint);

    //4 把值组合成一个json字符串
    cJSON *root2 = cJSON_CreateObject();
    cJSON_AddStringToObject(root2,"name","li4");
    cJSON_AddNumberToObject(root2,"age",16);

    char *json2 = cJSON_PrintUnformatted(root2);

    printf("json2:%s\n",json2);  
    
    
    //5 释放内存
    cJSON_Delete(root);
    cJSON_Delete(root2);


     
    return 0;
}
